<?php

class Trang extends MY_Admin
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();

		// Check if the logged user is an administrator
		//$this->access_library->check_access();

		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'trang_model', 'trang');
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'trang');
		$this->user = $this->access->get_user();
		$this->load->helper('url'); //You should autoload this one ;)
		
	}

	// Public methods
	public function index()
	{
		$this->access->restrict('Admin.Website.Trang.Index');
		$this->load->library('pagination');
		$this->load->helper('url');
		$this->load->helper('text');
		$config = array();
		$config['base_url'] = base_url().'admin/website/trang';
		$config['total_rows'] = $this->trang->count_all_trang();
		$config['per_page'] = 30;
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		
		$lang_working = load_language_working();
		$data['title']		=	'Quản lý Trang';
		$data['contents'] 		= $this->trang->get_all_trang($config['per_page'], $page);
		$data['count']				= $this->trang->count_all_trang();
		$data['links'] = $this->pagination->create_links();
		$this->_template['page']	= "website/trang/list";
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	
	
	public function add()
	{
		$this->access->restrict('Admin.Website.Trang.Add');
		$data['title']	=	'Thêm bài viết mới';
		$lang_working = load_language_working();
		$this->load->helper($this->config->item('wysiwyg'));
		$this->form_validation->set_rules('title', 'lang:form_title_page', 'required|max_length[250]|xss_clean');
		$this->form_validation->set_rules('alias', 'lang:form_alias_page', 'required|max_length[250]|xss_clean|callback_check_alias');
		$this->form_validation->set_rules('description', 'lang:form_description', 'max_length[200]|xss_clean');
		$this->form_validation->set_rules('content', 'lang:form_content', 'trim|required');
		$this->form_validation->set_error_delimiters('', '<br />');
		$data['error'] = '';
		
		if ($this->form_validation->run() == TRUE)
		{
			$this->trang->add_trang($data);
			$this->session->set_flashdata('message', lang('successfully_edited'));
			redirect('admin/website/trang', 'refresh');
		}
		
		$this->_template['page']	= 'website/trang/add';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function edit($id = null)
	{
		$this->access->restrict('Admin.Website.Trang.Edit');
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		
		$this->load->helper($this->config->item('wysiwyg'));
		$data['error'] = '';
		$data['title']	=	'Chỉnh sửa bài viết';
		$lang_working = load_language_working();
		$data['info'] = $this->trang->get_trang($id);
		if($data['info']->alias === strtolower(auto_alias($this->input->post('alias'))) || $this->input->post('alias')=='')
		{
			$this->form_validation->set_rules('alias', 'lang:form_alias_page', 'max_length[160]|xss_clean');
		}
		else
		{
			$this->form_validation->set_rules('alias', 'lang:form_alias_page', 'max_length[160]|xss_clean|callback_check_alias');
		}
		//$data['categories'] = array_remove_item_id($this->category->get_categories($lang_working['id']),$id);	
		$this->form_validation->set_rules('title', 'lang:form_title_page', 'required|max_length[60]|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'max_length[200]|xss_clean');
		$this->form_validation->set_rules('content', 'lang:form_content', 'trim|required');
		$this->form_validation->set_error_delimiters('', '<br />');

		
		if ($this->form_validation->run() == TRUE)
		{
			$this->trang->edit_trang($id);
			$this->session->set_flashdata('message', lang('successfully_edited'));
			redirect('admin/website/trang', 'refresh');
		}
		
		$this->_template['page']	= 'website/trang/edit';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function search()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Tìm kiếm thông tin';
		$data['description'] = 'Tìm kiếm thông tin';
		$data['search_keyword'] = trim($this->input->post('keyword', TRUE));
		if($data['search_keyword']!=='')
		{
			$data['result'] = $this->trang->search_data($data['search_keyword']);
			$this->_template['page']	= 'website/trang/result';
			$this->system_library->load($this->_template['page'], $data, TRUE);
		}
		else
		{
			redirect('admin/website/trang', 'refresh');
		}
	}
	
	public function check_alias()
	{
        if($this->trang->check_alias())
        {
            $this->form_validation->set_message('check_alias','Alias already exist please choose onther one');
            return false;
        }
        else
        {
            
            return true;
        }
    }

	

	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->trang->change_status_trang($id,1);
		redirect('admin/website/trang', 'refresh');
		
	}
	
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->trang->change_status_trang($id,0);
		redirect('admin/website/trang', 'refresh');
	}
	public function delete($id)
	{
		$this->access->restrict('Admin.Website.Trang.Delete');
		$this->trang->delete_trang($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));

		redirect('admin/website/trang', 'refresh');
	}
}

/* End of file categories.php */
/* Location: ./application/modules/admin/controllers/categories.php */